package com.pangda.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pangda.common.constant.BusinessStatusConstant;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.pojo.Activity;
import com.pangda.pojo.Clue;
import com.pangda.pojo.Contract;
import com.pangda.pojo.dto.*;
import com.pangda.pojo.vo.ActivityVo;
import com.pangda.pojo.vo.ReportVo;
import com.pangda.service.IBusinessService;
import com.pangda.service.IClueService;
import com.pangda.service.IContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/report")
@Api(tags = "报表相关接口")
public class ReportController {
    @Autowired
    private IContractService contractService;

    @Autowired
    private IClueService clueService;



/*    @GetMapping("/contractStatisticsList")
    @ApiOperation("合同统计列表")
    public PageResult contractStatisticsList() {
        return null;
    }*/

    @GetMapping("/contractStatisticsList")
    @ApiOperation("客户统计列表")
    public PageResult contractStatisticsList(PageResult pageResult, ContractDto contractDto) {
        return contractService.contractStatisticsList(pageResult, contractDto);
    }

    @GetMapping("/subjectStatistics/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("学科客户分布")
    public Map<String,Object> subjectStatistics(@PathVariable("beginCreateTime") String beginCreateTime,
                                        @PathVariable("endCreateTime") String endCreateTime) {
        return null;
    }

    @GetMapping("/deptStatisticsList/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("销售额统计报表")
    public PageResult deptStatisticsList(@PathVariable("beginCreateTime") String beginCreateTime,
                                         @PathVariable("endCreateTime") String endCreateTime,
                                         DeptDataList deptDataList) {

        return contractService.deptStatisticsList(deptDataList);
    }
    @GetMapping("/salesStatistics/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("销售额统计图")
    public Map<String,Object> salesStatistics(@PathVariable("beginCreateTime") String beginCreateTime,
                                       @PathVariable("endCreateTime") String endCreateTime
                                       ) {

        return contractService.salesStatistics(beginCreateTime,endCreateTime);
    }

    @GetMapping("/contractStatistics/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("客户统计图")
    public Map<String,Object> contractStatistics(@PathVariable("beginCreateTime") String beginCreateTime,
                                              @PathVariable("endCreateTime") String endCreateTime
    ) {

        return contractService.contractStatistics(beginCreateTime,endCreateTime);
    }

    @GetMapping("/cluesStatisticsList")
    @ApiOperation("线索统计报表")
    public PageResult cluesStatisticsList(PageResult pageResult, ClueDto clueDto) {
        return clueService.cluesStatisticsList(pageResult, clueDto);
    }


    @GetMapping("/cluesStatistics/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("线索统计图")
    public Map<String,Object> cluesStatistics(@PathVariable("beginCreateTime") String beginCreateTime,
                                                 @PathVariable("endCreateTime") String endCreateTime
    ) {
        return clueService.cluesStatistics(beginCreateTime,endCreateTime);
    }

    @GetMapping("/activityStatisticsList")
    @ApiOperation("活动统计报表")
    public Map<String,Object> activityStatisticsList(ActivityDto dto,String code,String name,Integer total) {
        return null;
    }

    @GetMapping("/chanelStatistics/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("渠道统计图")
    public Map<String,Object> chanelStatistics(@PathVariable("beginCreateTime") String beginCreateTime,
                                              @PathVariable("endCreateTime") String endCreateTime
                                       )


    {
        return null;
    }

    @GetMapping("/activityStatistics/{beginCreateTime}/{endCreateTime}")
    @ApiOperation("活动统计图")
    public Map<String,Object> activityStatistics(@PathVariable("beginCreateTime") String beginCreateTime,
                                               @PathVariable("endCreateTime") String endCreateTime
    ) {
        return null;
    }



    @GetMapping("/getVulnerabilityMap/{beginCreateTime}/{endCreateTime}")
    public ResponseResult getVulnerabilityMap(@PathVariable("beginCreateTime") String beginCreateTime,
                                                   @PathVariable("endCreateTime") String endCreateTime) {
        ReportVo reportVo = new ReportVo();
        if (StrUtil.isBlank(beginCreateTime) && StrUtil.isBlank(endCreateTime)) {
            return ResponseResult.error("请选择时间");
        }
        LambdaQueryWrapper<Clue> clueWrapper = new LambdaQueryWrapper<>();
        clueWrapper.between(Clue::getCreateTime,beginCreateTime,endCreateTime+" 23:59:59");
        int cluesNums = clueService.count(clueWrapper);
        reportVo.setCluesNums(cluesNums);

        clueWrapper.eq(Clue::getStatus, BusinessStatusConstant.OPEN_OCEAN_POOLS);
        int effectiveCluesNums = clueService.count(clueWrapper);
        reportVo.setEffectiveCluesNums(effectiveCluesNums);

        clueWrapper.eq(Clue::getStatus, BusinessStatusConstant.TRANSFORM_TO_BUSINESS_SUCCESS);
        int businessNums = clueService.count(clueWrapper);
        reportVo.setBusinessNums(businessNums);



        LambdaQueryWrapper<Contract> contractWrapper = new LambdaQueryWrapper<>();
        contractWrapper.between(Contract::getCreateTime,beginCreateTime,endCreateTime+" 23:59:59");
        int contractNums = contractService.count(contractWrapper);
        reportVo.setContractNums(contractNums);


        return ResponseResult.success(reportVo);


    }

}
